'use strict';
import React, {Component} from 'react';
import {
    StyleSheet,
    View,
    ListView,
    ToastAndroid,
} from 'react-native';

import HttpUtil         from './util/HttpUtil';
import Person           from './entity/PersonBean';
import InputView        from './widget/InputView';
import QueryItemView    from './component/QueryItemView';
import {CreditUtil} from "../lib.xjs/util/CreditUtil";

export default class Ryhc extends Component {
    // static navigationOptions = {
    //     headerStyle: {
    //         // height: NAV_BAR_HEIGHT,
    //     }
    // };

    mHttpUtil   = null;
    mPersons    = [];

    constructor(props){
        super(props);
        const datas = new ListView.DataSource({rowHasChanged: (r1, r2) => {return r1 !== r2}});
        this.mHttpUtil = HttpUtil.getInstance();
        this.state = {
            mDatas  : datas.cloneWithRows(this.mPersons),
        }
    };

    render() {
        return (
            <View ref="root" style={{flex: 1, justifyContent: 'flex-end'}} >
               <ListView
                   style={{flex: 1}}
                   ref="list"
                   dataSource={this.state.mDatas}
                   renderRow={this._renderRow.bind(this)}
                   enableEmptySections = {true}/>
                <InputView ref="inputview" onPress = {(text) => this._doQuery(text)} />
            </View>
        );
    };

    _doQuery(id){
        if(!CreditUtil.isIDCardValid(id)) {
            ToastAndroid.show('兄弟，号码' + id + '不对啊！',ToastAndroid.SHORT);
            return;
        }

        let person = Person.getNote(id);
        // this.mPersons = this.mPersons.concat(person);
        this.mPersons = [...this.mPersons, person];

        ToastAndroid.show("this.mPersons.length:" + this.mPersons.length, ToastAndroid.SHORT);

        this.setState({
            mDatas: this.state.mDatas.cloneWithRows(this.mPersons)
        });

        this.mHttpUtil.doLoad(id, (status, text = '') => {
            switch (status) {
                case Person.QUERYING:{
                    person.mStatus = Person.QUERYING;
                    this._scrollToEnd();
                    break;
                }

                case Person.RETURN_SUCCESS:{
                    if(text === 'null' || text === null || typeof text === 'undefined') {
                        Person.formatFail(person);
                    }
                    else{
                        try {
                            let result = JSON.parse(text);
                            Person.formatSuccess(person, result);
                        }
                        catch (err) {
                            alert(err + '\n解析错误:' + text)
                            Person.formatFail(person);
                        }
                    }
                    // this.mPersons = [...this.mPersons];
                    this.mPersons = this.mPersons.splice(0);
                    this.setState({
                        mDatas: this.state.mDatas.cloneWithRows(this.mPersons)
                    });
                    this._scrollToEnd();
                    break;
                }

                case Person.RETURN_FAIL:{
                    alert(text);
                    let result = JSON.parse(text);
                    Person.formatFail(person, result);
                    // this.mPersons = [...this.mPersons];
                    this.mPersons = this.mPersons.splice(0);
                    this.setState({
                        mDatas: this.state.mDatas.cloneWithRows(this.mPersons)
                    });
                    this._scrollToEnd();
                    console.warn(text);
                    break;
                }
            }
        });
    }

    /** 滚动到列表末尾 */
    _scrollToEnd(){
        setTimeout(
            () => {
                this.refs.list.getScrollResponder().scrollToEnd();
                this.refs.list.getScrollResponder().scrollTo({x: 0, y: 10000, animated: true});
            }, 100);
    }

    _renderRow(rowData, sectionID, rowID){
        return <QueryItemView people={rowData}/>;
    }
}
